package Service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import file.FileGestion;
import series.Serie;
import singleton.ConfigPropertiesSingleton;
import singleton.SeriesPropertiesSingleton;
import Exception.PropertiesException;

public class SeriesService {
    
    private static String REG_EXP_EPISODE = "[E]{1}[0-9]{2}";
    private static String REG_EXP_SAISON = "[S]{1}[0-9]{2}";
    
    private SeriesPropertiesSingleton serieService;
    private Properties propSeries;
    
    private ConfigService configService;

    
    public SeriesService() throws PropertiesException {
        try {
            serieService = SeriesPropertiesSingleton.getSeriesProperties();
            propSeries = serieService.getProp();
        } catch (FileNotFoundException e) {
            throw new PropertiesException("Fichier introuvable");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    
    public List<Serie> getAllSeries(List<String> series) throws FileNotFoundException, IOException, PropertiesException{
        List<Serie> seriesBO = new ArrayList<Serie>();
        for(String serie : series){
            Serie serieBO = Serie.isInSerie(serie);
            if(serieBO != null){
                String path = configService.getPath();
                File old = new File(path + "/" + serieBO.getOld_name());
                File newFile = new File(path + "/" + serieBO.toString());
                if(old.renameTo(newFile)){
                    seriesBO.add(serieBO);
                }
            }
        }
        return seriesBO;
    }
    
    public String getNom(String serie){
        Map<String, String> mapProperties = new HashMap<String, String>();
        for(Entry<Object, Object> x : propSeries.entrySet()) {
            mapProperties.put((String)x.getKey(), (String)x.getValue());
        }
        for(Entry<String, String> itemSerie : mapProperties.entrySet()) {
            if(compareSeries(serie, itemSerie.getValue())){
                return itemSerie.getKey();
            }
        }
        return null;
    }
    
    public int getEpisode(String serie){
        try{
            Pattern p = Pattern .compile(REG_EXP_EPISODE);
            Matcher m = p.matcher(serie);
            while (m.find())
               return Integer.valueOf(serie.substring(m.start()+1, m.end()));
         }catch(PatternSyntaxException pse){
         }
        return -1;
    }
    
    public int getSaison(String serie){
        try{
            Pattern p = Pattern .compile(REG_EXP_SAISON);
            Matcher m = p.matcher(serie);
            while (m.find())
               return Integer.valueOf(serie.substring(m.start()+1, m.end()));
         }catch(PatternSyntaxException pse){
         }
        return -1;
    }
    
    private boolean compareSeries(String serie, String value){
        List<String> listRecup = new ArrayList<String>();
        String[] tabRecup = value.split("\\|");
        listRecup.addAll(Arrays.asList(tabRecup));
        for(String item : listRecup){
            if(serie.toUpperCase().contains(item.toUpperCase())){
                return true;
            }
        }
        return false;
        
    }
    
    public boolean moveFile(List<Serie> series){
        boolean res = false;
        ConfigService config = null;
        try {
            config = new ConfigService();
        } catch (PropertiesException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String pathDD = config.getPathDD();
        String pathOld = config.getPath();
        File fil = new File(pathDD);
        if(fil.exists()){
            for(Serie serie : series){
                String name = serie.toString();
                String newPath = pathDD + "/" + serie.getNomSerie() + "/Saison" + serie.getSaison();
                File fileSaison = new File(newPath);
                File fileOld = new File(pathOld + "/" + name);
                if(fileSaison.exists()){
                    res = FileGestion.moveThisFileWithName(pathOld,newPath,serie.toString());
                }
            }
        }
        return res;
    }

}
